package o;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Matrix.ScaleToFit;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.huawei.appgallery.common.media.widget.zoomview.ScaleView;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import java.lang.ref.WeakReference;

public class blx implements OnTouchListener, o.blv.e, OnDoubleTapListener, OnGlobalLayoutListener {
    static final boolean ˎ = Log.isLoggable("ScaleViewAttacher", 3);
    private WeakReference<ImageView> ʻ;
    private a ʻॱ;
    private float ʼ = 1.0f;
    private OnLongClickListener ʼॱ;
    private boolean ʽ = true;
    private int ʽॱ;
    private int ʾ;
    private int ʿ;
    private int ˈ;
    private c ˉ;
    private boolean ˊ = false;
    private OnClickListener ˊˊ;
    private boolean ˊˋ;
    private final Matrix ˊॱ = new Matrix();
    private ScaleType ˊᐝ = ScaleType.FIT_CENTER;
    private float ˋ = 3.0f;
    private int ˋˊ = 2;
    private float ˋˋ;
    private GestureDetector ˋॱ;
    private float ˌ;
    private long ˍ = 0;
    private float ˏ = 1.0f;
    private final Matrix ˏॱ = new Matrix();
    private blv ͺ;
    private float ॱ = 1.0f;
    private final Matrix ॱˊ = new Matrix();
    private final float[] ॱˋ = new float[9];
    private final RectF ॱˎ = new RectF();
    private ViewTreeObserver ॱॱ;
    private e ॱᐝ;
    private float ᐝ = 1.0f;
    private d ᐝॱ;

    static /* synthetic */ class AnonymousClass4 {
        static final /* synthetic */ int[] ˏ = new int[ScaleType.values().length];

        static {
            try {
                ˏ[ScaleType.MATRIX.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                ˏ[ScaleType.FIT_START.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                ˏ[ScaleType.FIT_END.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                ˏ[ScaleType.FIT_CENTER.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                ˏ[ScaleType.FIT_XY.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
        }
    }

    public interface a {
        void ˋ(View view, float f, float f2);
    }

    class b implements Runnable {
        final /* synthetic */ blx ˊ;
        private final float ˋ;
        private final float ˎ;
        private final float ˏ;
        private final float ॱ;

        public b(blx o_blx, float f, float f2, float f3, float f4) {
            this.ˊ = o_blx;
            this.ˎ = f2;
            this.ˏ = f3;
            this.ˋ = f4;
            if (f < f2) {
                this.ॱ = 1.07f;
            } else {
                this.ॱ = 0.93f;
            }
        }

        public void run() {
            View ˋ = this.ˊ.ˋ();
            if (ˋ != null) {
                this.ˊ.ˊॱ.postScale(this.ॱ, this.ॱ, this.ˏ, this.ˋ);
                this.ˊ.ॱॱ();
                float ˏ = this.ˊ.ˏ();
                if ((this.ॱ <= 1.0f || ˏ >= this.ˎ) && (this.ॱ >= 1.0f || this.ˎ >= ˏ)) {
                    float f = this.ˎ / ˏ;
                    this.ˊ.ˊॱ.postScale(f, f, this.ˏ, this.ˋ);
                    this.ˊ.ॱॱ();
                    return;
                }
                blr.ˋ(ˋ, this);
            }
        }
    }

    class c implements Runnable {
        private final bly ˊ;
        private int ˋ;
        private int ˎ;
        final /* synthetic */ blx ˏ;

        public c(blx o_blx, Context context) {
            this.ˏ = o_blx;
            this.ˊ = bly.ˏ(context);
        }

        public void ˏ() {
            if (blx.ˎ) {
                Log.d("ScaleViewAttacher", "Cancel Fling");
            }
            this.ˊ.ˎ(true);
        }

        public void ˋ(int i, int i2, int i3, int i4) {
            RectF ॱ = this.ˏ.ॱ();
            if (ॱ != null) {
                int round;
                int i5;
                int round2;
                int i6;
                int round3 = Math.round(-ॱ.left);
                if (((float) i) < ॱ.width()) {
                    round = Math.round(ॱ.width() - ((float) i));
                    i5 = 0;
                } else {
                    round = round3;
                    i5 = round3;
                }
                int round4 = Math.round(-ॱ.top);
                if (((float) i2) < ॱ.height()) {
                    round2 = Math.round(ॱ.height() - ((float) i2));
                    i6 = 0;
                } else {
                    round2 = round4;
                    i6 = round4;
                }
                this.ˎ = round3;
                this.ˋ = round4;
                if (blx.ˎ) {
                    Log.d("ScaleViewAttacher", "fling. StartX:" + round3 + " StartY:" + round4 + " MaxX:" + round + " MaxY:" + round2);
                }
                if (round3 != round || round4 != round2) {
                    this.ˊ.ˋ(round3, round4, i3, i4, i5, round, i6, round2, 0, 0);
                }
            }
        }

        public void run() {
            View ˋ = this.ˏ.ˋ();
            if (ˋ != null && this.ˊ.ˊ()) {
                int ˋ2 = this.ˊ.ˋ();
                int ˎ = this.ˊ.ˎ();
                if (blx.ˎ) {
                    Log.d("ScaleViewAttacher", "fling run(). CurrentX:" + this.ˎ + " CurrentY:" + this.ˋ + " NewX:" + ˋ2 + " NewY:" + ˎ);
                }
                this.ˏ.ˊॱ.postTranslate((float) (this.ˎ - ˋ2), (float) (this.ˋ - ˎ));
                this.ˏ.ˊ(this.ˏ.ʼ());
                this.ˎ = ˋ2;
                this.ˋ = ˎ;
                blr.ˋ(ˋ, this);
            }
        }
    }

    public interface d {
        void ˎ(RectF rectF);
    }

    public interface e {
        void ˋ(View view, float f, float f2);
    }

    private static boolean ˋ(ScaleType scaleType) {
        if (scaleType == null) {
            return false;
        }
        switch (AnonymousClass4.ˏ[scaleType.ordinal()]) {
            case 1:
                throw new IllegalArgumentException(scaleType.name() + " is not supported in ScaleView");
            default:
                return true;
        }
    }

    private static void ˊ(ImageView imageView) {
        if (imageView != null && !(imageView instanceof ScaleView)) {
            imageView.setScaleType(ScaleType.MATRIX);
        }
    }

    public blx(ImageView imageView) {
        this.ʻ = new WeakReference(imageView);
        imageView.setOnTouchListener(this);
        this.ॱॱ = imageView.getViewTreeObserver();
        if (this.ॱॱ != null) {
            this.ॱॱ.addOnGlobalLayoutListener(this);
        }
        this.ˊˊ = null;
        ˊ(imageView);
        if (!imageView.isInEditMode()) {
            this.ͺ = blv.ˊ(imageView.getContext(), this);
            this.ˋॱ = new GestureDetector(imageView.getContext(), new 2(this));
            this.ˋॱ.setOnDoubleTapListener(this);
            ˋ(true);
        }
    }

    public final void ˎ() {
        if (this.ʻ != null) {
            ImageView imageView = (ImageView) this.ʻ.get();
            if (imageView != null) {
                ViewTreeObserver viewTreeObserver = imageView.getViewTreeObserver();
                if (viewTreeObserver != null) {
                    viewTreeObserver.removeGlobalOnLayoutListener(this);
                }
            }
        }
        this.ॱॱ = null;
        this.ᐝॱ = null;
        this.ॱᐝ = null;
        this.ʻॱ = null;
        this.ʻ = null;
    }

    public final RectF ॱ() {
        ˊॱ();
        return ˋ(ʼ());
    }

    public final ImageView ˋ() {
        ImageView imageView = null;
        if (this.ʻ != null) {
            imageView = (ImageView) this.ʻ.get();
        }
        if (imageView == null) {
            ˎ();
        }
        return imageView;
    }

    public void ॱ(float f) {
        this.ॱ = f;
    }

    public void ˏ(float f) {
        this.ʼ = f;
    }

    public final float ˏ() {
        return ˎ(this.ॱˊ, 0);
    }

    public final ScaleType ˊ() {
        return this.ˊᐝ;
    }

    public final boolean onDoubleTap(MotionEvent motionEvent) {
        if (!this.ˊ) {
            return false;
        }
        try {
            float ˏ = ˏ();
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            if (ॱ(this.ॱ, this.ʼ) && ॱ(this.ॱ, this.ᐝ)) {
                ˎ(ˏ, this.ॱ, 1.3333f * this.ॱ, x, y);
            } else if (ॱ(this.ॱ, this.ʼ) && !ॱ(this.ॱ, this.ᐝ)) {
                ˎ(ˏ, this.ॱ, this.ᐝ, x, y);
            } else if (ॱ(this.ʼ, this.ᐝ) && !ॱ(this.ॱ, this.ʼ)) {
                ˎ(ˏ, this.ॱ, this.ʼ, x, y);
            } else if (ॱ(this.ॱ, this.ᐝ) && !ॱ(this.ॱ, this.ʼ)) {
                ˎ(ˏ, this.ॱ, this.ʼ, x, y);
            } else if (ॱ(ˏ, this.ॱ)) {
                ˎ(this.ʼ, x, y);
            } else if (ॱ(ˏ, this.ʼ)) {
                ˎ(this.ᐝ, x, y);
            } else {
                ˎ(this.ॱ, x, y);
            }
            return true;
        } catch (ArrayIndexOutOfBoundsException e) {
            return false;
        }
    }

    private void ˎ(float f, float f2, float f3, float f4, float f5) {
        if (ॱ(f, f2)) {
            ˎ(f3, f4, f5);
        } else {
            ˎ(f2, f4, f5);
        }
    }

    private boolean ॱ(float f, float f2) {
        return ((double) Math.abs(f - f2)) <= 0.1d;
    }

    public final boolean onDoubleTapEvent(MotionEvent motionEvent) {
        return false;
    }

    public final void ˎ(float f, float f2) {
        ImageView ˋ = ˋ();
        if (this.ˊ) {
            this.ˊॱ.postTranslate(f, f2);
            ॱॱ();
            if (this.ʽ && !this.ͺ.ˏ()) {
                if (this.ˋˊ == 2 || ((this.ˋˊ == 0 && f >= 1.0f) || (this.ˋˊ == 1 && f <= SizeModifier.STABLE_STATE_SCALE))) {
                    ViewParent parent = ˋ.getParent();
                    if (parent != null) {
                        parent.requestDisallowInterceptTouchEvent(false);
                    }
                }
            }
        }
    }

    public final void ˋ(float f, float f2, float f3, float f4) {
        if (ˎ) {
            Log.d("ScaleViewAttacher", "onFling. sX: " + f + " sY: " + f2 + " Vx: " + f3 + " Vy: " + f4);
        }
        ImageView ˋ = ˋ();
        if (this.ˊ) {
            this.ˉ = new c(this, ˋ.getContext());
            this.ˉ.ˋ(ˋ.getWidth(), ˋ.getHeight(), (int) f3, (int) f4);
            ˋ.post(this.ˉ);
        }
    }

    public final void onGlobalLayout() {
        ImageView ˋ = ˋ();
        if (ˋ != null && this.ˊˋ) {
            int top = ˋ.getTop();
            int right = ˋ.getRight();
            int bottom = ˋ.getBottom();
            int left = ˋ.getLeft();
            if (top != this.ˈ || bottom != this.ʿ || left != this.ʾ || right != this.ʽॱ) {
                ˊ(ˋ.getDrawable());
                this.ˈ = top;
                this.ʽॱ = right;
                this.ʿ = bottom;
                this.ʾ = left;
            }
        }
    }

    public final void ˎ(OnClickListener onClickListener) {
        this.ˊˊ = onClickListener;
    }

    public final void ॱ(float f, float f2, float f3) {
        if (!this.ˊ) {
            return;
        }
        if (ˏ() < this.ˋ || f < 1.0f) {
            this.ˊॱ.postScale(f, f, f2, f3);
            ॱॱ();
        }
    }

    public final boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        View ˋ = ˋ();
        if (ˋ != null) {
            if (this.ॱᐝ != null) {
                RectF ॱ = ॱ();
                if (ॱ != null) {
                    float x = motionEvent.getX();
                    float y = motionEvent.getY();
                    if (ॱ.contains(x, y)) {
                        this.ॱᐝ.ˋ(ˋ, (x - ॱ.left) / ॱ.width(), (y - ॱ.top) / ॱ.height());
                        return true;
                    }
                }
            }
            if (this.ʻॱ != null) {
                this.ʻॱ.ˋ(ˋ, motionEvent.getX(), motionEvent.getY());
            }
        }
        return false;
    }

    public final boolean onTouch(View view, MotionEvent motionEvent) {
        boolean z = false;
        if (!this.ˊˋ) {
            return false;
        }
        switch (motionEvent.getAction()) {
            case 0:
                ˊ(view, motionEvent);
                break;
            case 1:
            case 3:
                z = ॱ(view, motionEvent, false);
                break;
        }
        if (!this.ˊ) {
            return z;
        }
        if (this.ˋॱ != null && this.ˋॱ.onTouchEvent(motionEvent)) {
            z = true;
        }
        if (this.ͺ == null || !this.ͺ.ˏ(motionEvent)) {
            return z;
        }
        return true;
    }

    private boolean ॱ(View view, MotionEvent motionEvent, boolean z) {
        if (this.ˊ && ˏ() < this.ˏ) {
            Log.d("ScaleView", "getCurrentScale() < mMinScale");
            RectF ॱ = ॱ();
            if (ॱ != null) {
                view.post(new b(this, ˏ(), this.ˏ, ॱ.centerX(), ॱ.centerY()));
                z = true;
            }
        }
        if (Double.compare((double) motionEvent.getX(), (double) this.ˋˋ) == 0 && Double.compare((double) motionEvent.getY(), (double) this.ˌ) == 0 && System.currentTimeMillis() - this.ˍ > 500) {
            this.ˍ = System.currentTimeMillis();
            if (this.ˊˊ != null) {
                this.ˊˊ.onClick(ˋ());
            }
        }
        return z;
    }

    private void ˊ(View view, MotionEvent motionEvent) {
        ViewParent parent = view.getParent();
        if (parent != null && this.ˊ) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        this.ˋˋ = motionEvent.getX();
        this.ˌ = motionEvent.getY();
        ᐝ();
    }

    public void ˏ(boolean z) {
        this.ʽ = z;
    }

    public void ˋ(float f) {
        this.ˏ = f;
    }

    public void ˎ(float f) {
        this.ˋ = f;
    }

    public final void ॱ(OnLongClickListener onLongClickListener) {
        this.ʼॱ = onLongClickListener;
    }

    public final void ˋ(d dVar) {
        this.ᐝॱ = dVar;
    }

    public final void ˊ(e eVar) {
        this.ॱᐝ = eVar;
    }

    public final void ˏ(a aVar) {
        this.ʻॱ = aVar;
    }

    public final void ॱ(ScaleType scaleType) {
        if (ˋ(scaleType) && scaleType != this.ˊᐝ) {
            ʽ();
        }
    }

    public final void ˋ(boolean z) {
        this.ˊˋ = z;
        ʽ();
    }

    public final void ʽ() {
        ImageView ˋ = ˋ();
        if (ˋ == null) {
            return;
        }
        if (this.ˊˋ) {
            ˊ(ˋ);
            ˊ(ˋ.getDrawable());
            return;
        }
        ͺ();
    }

    public final void ˎ(float f, float f2, float f3) {
        ImageView ˋ = ˋ();
        if (ˋ != null) {
            ˋ.post(new b(this, ˏ(), f, f2, f3));
        }
    }

    protected Matrix ʼ() {
        this.ॱˊ.set(this.ˏॱ);
        this.ॱˊ.postConcat(this.ˊॱ);
        return this.ॱˊ;
    }

    private void ᐝ() {
        if (this.ˉ != null) {
            this.ˉ.ˏ();
            this.ˉ = null;
        }
    }

    private void ॱॱ() {
        ˊॱ();
        ˊ(ʼ());
    }

    private void ॱˊ() {
        ImageView ˋ = ˋ();
        if (ˋ != null && !(ˋ instanceof ScaleView) && ˋ.getScaleType() != ScaleType.MATRIX) {
            throw new IllegalStateException("The ImageView's ScaleType has been changed since attaching a ScaleViewAttacher");
        }
    }

    private void ˊॱ() {
        float f = 0.0f;
        ImageView ˋ = ˋ();
        if (ˋ != null) {
            RectF ˋ2 = ˋ(ʼ());
            if (ˋ2 != null) {
                float height = ˋ2.height();
                float width = ˋ2.width();
                height = ˋ(ˋ2, height, 0.0f, ˋ.getHeight());
                int width2 = ˋ.getWidth();
                if (width <= ((float) width2)) {
                    switch (AnonymousClass4.ˏ[this.ˊᐝ.ordinal()]) {
                        case 2:
                            f = -ˋ2.left;
                            break;
                        case 3:
                            f = (((float) width2) - width) - ˋ2.left;
                            break;
                        default:
                            f = ((((float) width2) - width) / 2.0f) - ˋ2.left;
                            break;
                    }
                    this.ˋˊ = 2;
                } else if (ˋ2.left > 0.0f) {
                    this.ˋˊ = 0;
                    f = -ˋ2.left;
                } else if (ˋ2.right < ((float) width2)) {
                    f = ((float) width2) - ˋ2.right;
                    this.ˋˊ = 1;
                } else {
                    this.ˋˊ = -1;
                }
                this.ˊॱ.postTranslate(f, height);
            }
        }
    }

    private float ˋ(RectF rectF, float f, float f2, int i) {
        if (f <= ((float) i)) {
            switch (AnonymousClass4.ˏ[this.ˊᐝ.ordinal()]) {
                case 2:
                    return -rectF.top;
                case 3:
                    return (((float) i) - f) - rectF.top;
                default:
                    return ((((float) i) - f) / 2.0f) - rectF.top;
            }
        } else if (rectF.top > 0.0f) {
            return -rectF.top;
        } else {
            if (rectF.bottom < ((float) i)) {
                return ((float) i) - rectF.bottom;
            }
            return f2;
        }
    }

    private RectF ˋ(Matrix matrix) {
        ImageView ˋ = ˋ();
        if (ˋ != null) {
            Drawable drawable = ˋ.getDrawable();
            if (drawable != null) {
                this.ॱˎ.set(0.0f, 0.0f, (float) drawable.getIntrinsicWidth(), (float) drawable.getIntrinsicHeight());
                matrix.mapRect(this.ॱˎ);
                return this.ॱˎ;
            }
        }
        return null;
    }

    private float ˎ(Matrix matrix, int i) {
        matrix.getValues(this.ॱˋ);
        return this.ॱˋ[i];
    }

    private void ͺ() {
        this.ˊॱ.reset();
        ˊ(ʼ());
        ˊॱ();
    }

    private void ˊ(Matrix matrix) {
        ImageView ˋ = ˋ();
        if (ˋ != null) {
            ॱˊ();
            ˋ.setImageMatrix(matrix);
            if (this.ᐝॱ != null) {
                RectF ˋ2 = ˋ(matrix);
                if (ˋ2 != null) {
                    this.ᐝॱ.ˎ(ˋ2);
                }
            }
        }
    }

    private void ˊ(Drawable drawable) {
        ImageView ˋ = ˋ();
        if (ˋ != null && drawable != null) {
            float width = (float) ˋ.getWidth();
            float height = (float) ˋ.getHeight();
            int intrinsicWidth = drawable.getIntrinsicWidth();
            int intrinsicHeight = drawable.getIntrinsicHeight();
            this.ˏॱ.reset();
            float f = width / ((float) intrinsicWidth);
            float f2 = height / ((float) intrinsicHeight);
            if (this.ˊᐝ == ScaleType.CENTER) {
                this.ˏॱ.postTranslate((width - ((float) intrinsicWidth)) / 2.0f, (height - ((float) intrinsicHeight)) / 2.0f);
            } else if (this.ˊᐝ == ScaleType.CENTER_CROP) {
                f = Math.max(f, f2);
                this.ˏॱ.postScale(f, f);
                this.ˏॱ.postTranslate((width - (((float) intrinsicWidth) * f)) / 2.0f, (height - (((float) intrinsicHeight) * f)) / 2.0f);
            } else if (this.ˊᐝ == ScaleType.CENTER_INSIDE) {
                f = Math.min(1.0f, Math.min(f, f2));
                this.ˏॱ.postScale(f, f);
                this.ˏॱ.postTranslate((width - (((float) intrinsicWidth) * f)) / 2.0f, (height - (((float) intrinsicHeight) * f)) / 2.0f);
            } else {
                ˊ(this.ˊᐝ, new RectF(0.0f, 0.0f, (float) intrinsicWidth, (float) intrinsicHeight), new RectF(0.0f, 0.0f, width, height));
            }
            ͺ();
        }
    }

    private void ˊ(ScaleType scaleType, RectF rectF, RectF rectF2) {
        switch (AnonymousClass4.ˏ[scaleType.ordinal()]) {
            case 2:
                this.ˏॱ.setRectToRect(rectF, rectF2, ScaleToFit.START);
                return;
            case 3:
                this.ˏॱ.setRectToRect(rectF, rectF2, ScaleToFit.END);
                return;
            case 4:
                this.ˏॱ.setRectToRect(rectF, rectF2, ScaleToFit.CENTER);
                return;
            case 5:
                this.ˏॱ.setRectToRect(rectF, rectF2, ScaleToFit.FILL);
                return;
            default:
                return;
        }
    }

    public void ˊ(float f) {
        this.ᐝ = f;
    }

    public void ʻ() {
        if (this.ˊ) {
            ˎ(this.ॱ, 0.0f, 0.0f);
        }
    }

    public void ˎ(boolean z) {
        this.ˊ = z;
    }
}
